package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.FoodBaseDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.pojo.Sport;
import com.itheima.service.FoodBaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = FoodBaseService.class)
public class FoodBaseServiceImpl implements FoodBaseService {

    @Autowired
    private FoodBaseDao foodBaseDao;

    @Override
    public void addFoodList(List<Food> list) {

        for (Food food : list) {
            String name = food.getName();
            if(name==null || name.length() <= 0){
                continue;
            }
            Food resultFood = foodBaseDao.findByName(food.getName());
            if(resultFood!=null){
                foodBaseDao.updateByName(food);
            }
            if(resultFood==null){
                foodBaseDao.addOneFood(food);
            }
        }


        /*foodBaseDao.deleteFoodList();
        for (Food food : list) {
        foodBaseDao.addFoodList(food);
    }*/
}

    //条件/分页查询
    @Override
    public PageInfo findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<Food> foodList = foodBaseDao.findByCondition(queryString);
        System.out.println("实现类查询出的结果" + foodList);
        return new PageInfo(foodList);
    }

    /**
     * 添加单个膳食
     *
     * @param food
     */
    @Override
    public void addOneFood(Food food) {
        foodBaseDao.addOneFood(food);
    }

    @Override
    public Food findById(Integer id) {
        return foodBaseDao.findById(id);
    }

    @Override
    public void update(Food food) {
        foodBaseDao.update(food);
    }

    @Override
    public void deleteById(Integer id) {
        foodBaseDao.deleteById(id);
    }
}
